package com.cxj.musicplayer.adapter;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cxj.musicplayer.R;
import com.cxj.musicplayer.data.DataManager;
import com.cxj.musicplayer.musicsource.network.DownloadFrom163;

import java.io.IOException;
import java.util.Date;

public class SongListAdapter extends BaseAdapter {
    private Context mContext;
    private String[] mSongName;
    private String[] mAuthorName;
    private String[] mSongId;
    public SongListAdapter(Context context, String data, String lid) throws IOException {
        System.out.println(new Date().getTime());
        mContext = context;

        mSongName = data.split("♫")[0].split("☆");
        mAuthorName = data.split("♫")[1].split("☆");
        mSongId = data.split("♫")[2].split("☆");

        DataManager.array_data.remove("n_song_name");
        DataManager.array_data.remove("n_author_name");
        DataManager.array_data.remove("n_song_id");


        DataManager.array_data.put("n_song_name", mSongName);
        DataManager.array_data.put("n_author_name", mAuthorName);
        DataManager.array_data.put("n_song_id", mSongId);
    }

    @Override
    public int getCount() {
        return mSongId == null ? 0 : mSongId.length;
    }

    @Override
    public Object getItem(int i) {
        return mSongId == null ? null : mSongId[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.song_list, null);
            viewHolder = new ViewHolder();
            viewHolder.song_name = view.findViewById(R.id.song_name);
            viewHolder.author = view.findViewById(R.id.author);
            viewHolder.position = view.findViewById(R.id.position);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        String songName = mSongName[i].length() > 14 ? mSongName[i].substring(0, 14) + "..." : mSongName[i];
        String authorName = mAuthorName[i].length() > 14 ? mAuthorName[i].substring(0, 14) + "..." : mAuthorName[i];
        viewHolder.song_name.setText(songName);
        viewHolder.author.setText(authorName);
        viewHolder.position.setText(String.valueOf(i + 1));
        return view;
    }

    static class ViewHolder{
        TextView song_name;
        TextView position;
        TextView author;
    }

}
